<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        function openDB(params) {

            this.connect = window.indexedDB.open('DBName', 1);

            this.connect.onerror = (e) => {
                console.error(e);
                console.error('数据库连接失败');
            };

            this.connect.onsuccess = (e) => {
                console.log(e);
                console.log('数据库连接成功');

                this.db = e.target.result;


            };

            this.connect.onupgradeneeded = (e) => {
                console.log(e);
                console.log('版本变更');
                this.db = e.target.result;
                creatTable();
            };

        }
        openDB();


        function creatTable() {
            this.store = this.db.createObjectStore('defult', {
                keyPath: 'id',
                autoIncrement: true
            });
            this.store.createIndex('user', 'user', {
                unique: false
            });
        }
        setTimeout(() => {

            console.log('defult表创建成功');
            this.db.transaction('defult', 'readwrite').objectStore('defult').add({
                name: '测试1'
            });
        }, 3000);
    </script>
</body>

</html>